<script setup>
import { userLogout } from "@/apis/user/user";
import { ref, watch, onMounted } from "vue";
import { questFun } from "@/apis/ai";
import { useRoute } from "vue-router";

const dialogVisible = ref(false);
const route = useRoute();
/**
 * 动态菜单数组
 */
const menus = ref([]);

const data = ref({});
/**
 * 退出登录
 */
const quit = () => {
  route.push("/");
  // 清空缓存
  sessionStorage.clear;
};

// ai聊天窗口
const textarea = ref("");
const dialog = ref([]);

// 将提问封装为一个对象
const getTextareaObject = () => {
  return {
    quest: textarea.value,
  };
};

//提交按钮
const submit = async () => {
  dialog.value.push(textarea.value);
  flag.value = true;
  const result = await questFun(getTextareaObject());
  textarea.value = "";
  flag.value = false;
  dialog.value.push(result.data);
};

const activeIndex = ref("/index"); // 默认值
// 监听路由变化
watch(
  () => route.path,
  (newPath) => {
    activeIndex.value = newPath;
  },
  { immediate: true }
);

onMounted(() => {
  activeIndex.value = route.path;
});

const flag = ref(false);

// ai聊天窗口结束
</script>
<template>
  <el-container class="app-container">
    <el-header class="el-header">
      <el-menu
        mode="horizontal"
        :ellipsis="false"
        :router="true"
        :default-active="activeIndex"
        class="nav-menu"
      >
        <div class="logo-section">
          <span class="title">基于协同过滤算法的智慧校园活动系统</span>
          <span class="subtitle">前台系统</span>
        </div>

        <div class="menu-items">
          <el-menu-item index="/index">首页</el-menu-item>
          <el-menu-item index="/activity-signup">我的活动</el-menu-item>
          <el-menu-item index="/wish">心愿单</el-menu-item>
          <el-menu-item index="/collect">收藏夹</el-menu-item>
          <el-menu-item index="/activity-history">浏览历史</el-menu-item>
          <el-menu-item index="/activity-notification">消息中心</el-menu-item>
          <el-menu-item index="/user-center">个人中心</el-menu-item>
        </div>
      </el-menu>
    </el-header>
    <el-container class="content-container">
      <el-main class="main-content">
        <div class="router-view-container">
          <router-view />
        </div>
        <el-dialog
          v-model="dialogVisible"
          title="ai聊天 临时会话"
          width="1000"
          class="ai-chat-dialog"
          draggable
          top="10vh"
        >
          <div class="chat-messages-container">
            <div
              v-for="(item, index) in dialog"
              :key="item"
              class="message-wrapper"
            >
              <div class="message-header">
                <el-icon v-if="index % 2 === 0" size="25" class="floating-icon"
                  ><User
                /></el-icon>
                <el-icon v-else size="25" class="floating-icon"
                  ><Opportunity
                /></el-icon>
                <el-text class="mx-1" v-if="index % 2 === 0" type="success"
                  >(您)</el-text
                >
                <el-text class="mx-1" v-else type="primary">(AI)</el-text>:
              </div>
              <el-card
                shadow="hover"
                :class="[
                  'message-card',
                  { 'ai-message': index % 2 == 1 },
                  { 'user-message': index % 2 == 0 },
                ]"
              >
                {{ item }}
              </el-card>
            </div>
          </div>

          <template #footer>
            <el-divider />
            <el-input
              v-model="textarea"
              maxlength="3000"
              class="chat-input"
              placeholder="输入内容开始聊天......"
              show-word-limit
              type="textarea"
              @keydown.enter="submit"
            />
            <div class="dialog-footer">
              <el-button
                type="primary"
                @click="submit"
                :disabled="flag"
                class="send-button"
                >发送</el-button
              >
            </div>
          </template>
        </el-dialog>

        <!-- <div id="aibutton" @click="dialogVisible = true">
          <el-tooltip content="AI 聊天" placement="top">
            <img
              src="https://img.alicdn.com/imgextra/i1/O1CN01khO8o01EAwXZnPWhX_!!6000000000312-2-tps-144-144.png"
              alt="通义千问"
            />
          </el-tooltip>
        </div> -->
      </el-main>
    </el-container>
  </el-container>
</template>

<style lang="less" scoped>
// Global style overrides
::-webkit-scrollbar {
  width: 0;
  height: 0;
  display: none;
}

.app-container {
  height: 100vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.el-header {
  padding: 0;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.04);
  position: relative;
  z-index: 100;
  background: rgba(255, 255, 255, 0.96);
  backdrop-filter: blur(20px);
}

.nav-menu {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 64px;
  padding: 0 30px;
  border: none;

  :deep(.el-menu-item) {
    font-size: 15px;
    height: 64px;
    line-height: 64px;
    padding: 0 20px;
    color: #1d1d1f;
    transition: all 0.3s ease;

    &:hover,
    &.is-active {
      color: #0066ff;
      background: transparent;
      transform: translateY(-2px);
    }

    &.is-active {
      font-weight: 500;
      position: relative;

      &:after {
        content: "";
        position: absolute;
        bottom: 12px;
        left: 20px;
        right: 20px;
        height: 3px;
        background: linear-gradient(90deg, #0066ff, #2196f3);
        border-radius: 3px;
      }
    }
  }
}

.logo-section {
  display: flex;
  align-items: center;
  gap: 8px;
}

.title {
  font-size: 20px;
  font-weight: 600;
  background: linear-gradient(45deg, #1a237e, #3949ab);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.subtitle {
  font-size: 14px;
  color: #666;
  padding: 4px 12px;
  background: #f5f5f5;
  border-radius: 12px;
}

.menu-items {
  display: flex;
  gap: 8px;
}

.content-container {
  flex: 1;
  overflow: hidden;
}

.main-content {
  height: calc(100vh - 64px);
  padding: 30px;
  overflow-y: auto;
  scrollbar-width: none; /* Firefox */
  -ms-overflow-style: none; /* IE and Edge */
}

.router-view-container {
  max-width: 1200px;
  margin: 0 auto;
}

#aibutton {
  position: fixed;
  bottom: 30px;
  left: 30px;
  width: 56px;
  height: 56px;
  cursor: pointer;
  transition: all 0.4s ease;
  border-radius: 28px;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.12);
  overflow: hidden;
  z-index: 1000;

  &:hover {
    transform: translateY(-4px) scale(1.05);
    box-shadow: 0 12px 28px rgba(0, 0, 0, 0.2);
  }

  img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: all 0.3s ease;
  }
}

// AI dialog styles
.ai-chat-dialog {
  :deep(.el-dialog__header) {
    padding: 20px 24px;
    margin: 0;
    border-bottom: 1px solid #eee;
    background: linear-gradient(to right, #f8f9fa, #ffffff);
  }

  :deep(.el-dialog__body) {
    padding: 24px;
    max-height: 400px;
    overflow-y: auto;
    scrollbar-width: none;
    -ms-overflow-style: none;

    &::-webkit-scrollbar {
      display: none;
    }
  }

  :deep(.el-dialog__footer) {
    padding: 16px 24px 24px;
    background: #fafafa;
    border-top: 1px solid #f0f0f0;
  }
}

.chat-messages-container {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.message-wrapper {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.message-header {
  display: flex;
  align-items: center;
  margin-bottom: 4px;
}

.message-card {
  border-radius: 16px;
  padding: 16px;
  line-height: 1.6;
  transition: all 0.3s ease;
  margin: 0;
  width: 100%;

  &.user-message {
    background: #e5f1ff;
    border: none;
    color: #0050c8;
    box-shadow: 0 2px 8px rgba(0, 102, 255, 0.08);
    align-self: flex-start;
  }

  &.ai-message {
    background: #f8f9fa;
    border: none;
    color: #333;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
    align-self: flex-start;
  }
}

.floating-icon {
  margin-right: 8px;
  color: #666;
}

.chat-input {
  margin-bottom: 16px;

  :deep(.el-textarea__inner) {
    border-radius: 12px;
    padding: 12px 16px;
    resize: none;
    transition: all 0.3s ease;
    border: 1px solid #e0e0e0;

    &:focus {
      border-color: #0066ff;
      box-shadow: 0 0 0 2px rgba(0, 102, 255, 0.1);
    }
  }
}

.send-button {
  background: linear-gradient(45deg, #0066ff, #2196f3);
  border: none;
  border-radius: 8px;
  padding: 8px 20px;
  font-weight: 500;
  transition: all 0.3s ease;

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 102, 255, 0.2);
  }

  &:active {
    transform: translateY(0);
  }
}
</style>
